package com.rico.kok.service;

import com.rico.kok.entity.Collection_;
import com.rico.kok.entity.Post;
import com.rico.kok.entity.PostLabel;
import com.rico.kok.entity.povos.PostUser;
import com.rico.kok.entity.povos.UserPosts;
import com.rico.kok.mapper.LikeMapper;
import com.rico.kok.mapper.PostLabelMapper;
import com.rico.kok.mapper.PostMapper;
import com.rico.kok.service.interf.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private PostLabelMapper postLabelMapper;

    //插入
    @Override
    @Transactional
    public int insertPost(Post post,List list) {
        //插入post获取主键值
        postMapper.insertPost(post);
        List<PostLabel> labels=new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            //循环获取标签，生成PostLabel对象放入到集合中
            labels.add(new PostLabel(post.getPostId(),Integer.parseInt((String) list.get(i))));
        }

        return postLabelMapper.insertPostLabelList(labels);
    }


    //通过用户ID查询所有内容
    @Override
    public List<PostUser> selectPostById(Integer userId) {
        Post post = new Post();
        post.setUserId(userId);
        return postMapper.selectPostUser(post);
    }

    //批量查询内容
    @Override
    public ArrayList<PostUser> selectPosts(Post post) {
        return postMapper.selectPostUser(post);
    }

    //查询作者及其内容
    @Override
    public UserPosts selectUserPosts(Post post) {
        return postMapper.selectUserPosts(post);
    }

    //判定该内容是否是该用户发送
    @Override
    public List<Post> selectPostByPostIdAndUserId(Post post) {
        return postMapper.selectPosts(post);
    }

    //通过关注id获取内容
    @Override
    public List<PostUser> selectPostByPostIds(List<Integer> postIds) {
        return postMapper.selectPostsByIds(postIds);
    }

    @Override
    @Transactional
    public int deletePost(Post post) {
        return postMapper.updatePostDelete(post);
    }

}
